package com.blast.manager.ask

import android.content.Intent
import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import com.blast.manager.R
import com.blast.manager.adapter.AskAdapter
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lzy.okgo.model.HttpParams
import com.wxl.common.base.AbsActivity
import com.wxl.common.http.OkGoApi
import com.wxl.common.http.SerialHttpCallback
import com.wxl.common.room.entity.QuestionItemEntity
import com.wxl.common.room.helper.DBHelper
import com.wxl.common.util.NetWorkUtils
import com.wxl.common.util.ObservableUtil
import kotlinx.android.synthetic.main.activity_blast_ask_layout.*

class QuestionListActivity : AbsActivity() {

    var mAskAdapter: AskAdapter? = null
    var loadDialog:BasePopupView?=null

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_blast_ask_layout
    }

    override fun onCreateViewChanged() {
        setTopTitle("常见问题")
        questionView.layoutManager = LinearLayoutManager(this)
        questionPreBtn.isEnabled = false
        questionNextBtn.isEnabled = false
        loadAskListData()

        questionPreBtn.setOnClickListener {
            mAskAdapter?.movePrePage()
        }

        questionNextBtn.setOnClickListener {
            mAskAdapter?.moveNextPage()
        }
    }

    /**
     * 加载问题列表数据
     */
    private fun loadAskListData() {
        if (NetWorkUtils.isNetWorkAvailable(this)) {
            loadDialog = XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .asLoading("正在加载问题列表...")
                .show()
            addPop(loadDialog as BasePopupView)
            val map = HttpParams()
            map.put("action", "getQuestionDetails")
            OkGoApi.api.post()
                .params(map)
                .execute(object : SerialHttpCallback<QuestionItemEntity>("List") {
                    override fun loadDataSuccess(datas: ArrayList<QuestionItemEntity>) {
                        if (datas.isNotEmpty()) {
                            DBHelper.setNewAskList(object :
                                ObservableUtil.Companion.OnNextCallback<Boolean>() {
                                override fun onNext(t: Boolean) {
                                    loadDBAskListData()
                                }
                            }, *datas.toTypedArray())
                        } else {
                            loadDBAskListData()
                        }
                    }

                    override fun loadDataFailed(e: Throwable) {
                        loadDBAskListData()
                    }
                })
        } else {
            loadDBAskListData()
        }
    }


    /**
     * 加载本地问题列表
     */
    private fun loadDBAskListData() {
        DBHelper.queryAskList(object :
            ObservableUtil.Companion.OnNextCallback<List<QuestionItemEntity>>() {
            override fun onNext(t: List<QuestionItemEntity>) {
                loadDialog?.dismiss()
                if (mAskAdapter == null) {
                    mAskAdapter = AskAdapter(t)
                    mAskAdapter?.bindToRecyclerView(questionView)
                    mAskAdapter!!.setOnItemClickListener { adapter, view, position ->
                        val intent = Intent(this@QuestionListActivity,QuestionActivity::class.java)
                        intent.putExtra("id",t[position].id)
                        startActivity(intent)
                    }
                }
            }
        })
    }


    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        mAskAdapter?.onKeyDown(keyCode, event)
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish()
        }
        if(keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F1){
            checkEnable()
        }
        return true
    }


    private fun checkEnable(){
        if( mAskAdapter != null){
            questionPreBtn.isEnabled = !mAskAdapter!!.isFirstPage()
            questionNextBtn.isEnabled = !mAskAdapter!!.isLastPage()
        }
    }
}